<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class FORMAT
{

	/**
	 *  字符进行安全转向处理 
	 * @param string $request
	 * @param boolean $istext
	 */
	static function safe($request, $istext = false)
	{
		// If we are on PHP >= 6.0.0 we do not need some code
		if (version_compare(PHP_VERSION, '6.0.0', '<'))
		{
			$request = addslashes($request); //转意义
		}
		
		if ($istext === true)
		{
			$request = strip_tags(str_replace(array(
				'<', 
				'>'
			), array(
				'&lt;', 
				'&gt;'
			), $request)); //括号 
		}
		
		return $request;
	}

	/**
	 * 封装MYSQL安全字符处理
	 * 
	 * @param string $unescaped_string
	 */
	static function mysql_safe($unescaped_string)
	{
		return mysql_escape_string($unescaped_string);
	}

	/**
	 *
	 * 过滤跨站脚本过滤
	 */
	public static function filter_xss($string, $allowedtags = '', $disabledattributes = array(
			'onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 
			'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 
			'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 
			'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 
			'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 
			'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 
			'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 
			'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 
			'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 
			'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 
			'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 
			'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 
			'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'))
	{
		$string = preg_replace('/\s(' . implode('|', $disabledattributes) . ').*?([\s\>])/', '\\2', preg_replace('/<(.*?)>/ie', "'<'.preg_replace(array('/javascript:[^\"\']*/i', '/(" . implode('|', $disabledattributes) . ")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", $string));
		
		return $string;
	}

	/**
	 * 
	 *替换字符串
	 * @param string $string
	 * 
	 * @return string 
	 */
	public static function safe_replace($string)
	{
		$string = str_replace('%20', '', $string);
		$string = str_replace('%27', '', $string);
		$string = str_replace('*', '', $string);
		$string = str_replace('"', '&quot;', $string);
		$string = str_replace("'", '', $string);
		$string = str_replace("\"", '', $string);
		$string = str_replace('//', '', $string);
		$string = str_replace(';', '', $string);
		$string = str_replace('<', '&lt;', $string);
		$string = str_replace('>', '&gt;', $string);
		$string = str_replace('(', '', $string);
		$string = str_replace(')', '', $string);
		$string = str_replace("{", '', $string);
		$string = str_replace('}', '', $string);
		
		return $string;
	}

	/**
	 * 
	 * 格式化内容显示
	 * @param string $content
	 * @param int    $charset_num
	 * 
	 * @return array $content = array('content_title' => 截断后内容, 'content_content' => 实际内容 )
	 */
	public static function format_content($content, $charset_num = 200, $dot = '...')
	{
		$content_title = '';
		$content_conent = strip_tags($content);
		$content_len = strlen($content_conent);
		
		if ($content_len > $charset_num)
		{
			$content_title = cjk_substr($content_conent, $charset_num, $dot);
		}
		else
		{
			$content_title = '';
		}
		
		return array(
			'content_title' => $content_title, 
			'content_content' => $content
		);
	}

	public static function clear_html($content)
	{
		$content = strip_tags($content);
		$content = preg_replace("/<a[^>]*>/i", "", $content);
		$content = preg_replace("/<\/a>/i", "", $content);
		$content = preg_replace("/<div[^>]*>/i", "", $content);
		$content = preg_replace("/<\/div>/i", "", $content);
		$content = preg_replace("/<!--[^>]*-->/i", "", $content); //注释内容  
		$content = preg_replace("/style=.+?['|\"]/i", '', $content); //去除样式  
		$content = preg_replace("/class=.+?['|\"]/i", '', $content); //去除样式  
		$content = preg_replace("/id=.+?['|\"]/i", '', $content); //去除样式     
		$content = preg_replace("/lang=.+?['|\"]/i", '', $content); //去除样式      
		$content = preg_replace("/width=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/height=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/border=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/face=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/face=.+?['|\"]/", '', $content); //去除样式 只允许小写 正则匹配没有带 i 参数
		return $content;
	}

	public static function clear_body_html($content)
	{
		$content = preg_replace("/<span[^>]*>/i", "", $content);
		$content = preg_replace("/<\/span>/i", "", $content);
		$content = preg_replace("/<a[^>]*>/i", "", $content);
		$content = preg_replace("/<\/a>/i", "", $content);
		$content = preg_replace("/<div[^>]*>/i", "", $content);
		$content = preg_replace("/<\/div>/i", "", $content);
		$content = preg_replace("/<!--[^>]*-->/i", "", $content); //注释内容  
		$content = preg_replace("/style=.+?['|\"]/i", '', $content); //去除样式  
		$content = preg_replace("/class=.+?['|\"]/i", '', $content); //去除样式  
		$content = preg_replace("/id=.+?['|\"]/i", '', $content); //去除样式     
		$content = preg_replace("/lang=.+?['|\"]/i", '', $content); //去除样式      
		$content = preg_replace("/width=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/height=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/border=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/face=.+?['|\"]/i", '', $content); //去除样式   
		$content = preg_replace("/face=.+?['|\"]/", '', $content); //去除样式 只允许小写 正则匹配没有带 i 参数
		return $content;
	}

	static function parse_links($str, $popup = TRUE)
	{
		$str = @preg_replace('/(http[s]?:\/\/(?!' . $_SERVER['HTTP_HOST'] . ')[-a-zA-Z0-9@:;%_\+.~#?\&\/\/=]+)/i', '<a href="\1" rel="nofollow" target="_blank">\1</a>', $str);

		$str = @preg_replace('/(http[s]?:\/\/'.$_SERVER['HTTP_HOST'].'[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]*)/i', '<a href="\1">\1</a>', $str);
		
		if (strpos($str, "http") === FALSE)
		{
			$str = @preg_replace('/((?!' . $_SERVER['HTTP_HOST'] . ')www\.[-a-zA-Z0-9@:;%_\+\.~#?&\/\/=]+)/i', '<a href="http://\1" rel="nofollow" target="_blank">\1</a>', $str);
			
			if(preg_match('/^www\./', $_SERVER['HTTP_HOST']))
			{
				$str = @preg_replace('/('.$_SERVER['HTTP_HOST'].'[-a-zA-Z0-9@:;%_\+\.~#?&\/\/=]*)/i', '<a href="http://\1">\1</a>', $str);
			}
		}
		
		$str = @preg_replace('/([a-z0-9\+_\-]+[\.]?[a-z0-9\+_\-]+@[a-z0-9\-]+\.+[a-z]{2,6})/is', '<a href="mailto:\1">\1</a>', $str);	//解析EMAIL地址
		
		return $str;
	}

	static function outside_url_exists($str)
	{
		if(preg_match('/(http[s]?:\/\/(?!' . $_SERVER['HTTP_HOST'] . ')[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]+)/i', $str))
		{
			return true;
		}
		else
		{
			if(preg_match('/((?!' . $_SERVER['HTTP_HOST'] . ')www\.[-a-zA-Z0-9@:;%_\+\.~#?&\/\/=]+)/i', $str))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}

}